package com.example.writting.ui.wordfill.adapters

import android.content.res.ColorStateList
import android.graphics.Color
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.example.writting.databinding.ItemSupplementBinding
import com.example.writting.model.Supplement
import com.google.android.material.chip.Chip

class SupplementAdapter : RecyclerView.Adapter<SupplementAdapter.SupplementViewHolder>() {

    private val items = mutableListOf<Supplement>()

    fun updateItems(newItems: List<Supplement>) {
        val diffCallback = object : DiffUtil.Callback() {
            override fun getOldListSize() = items.size
            override fun getNewListSize() = newItems.size
            override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int) =
                items[oldItemPosition].sentence == newItems[newItemPosition].sentence
            override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int) =
                items[oldItemPosition] == newItems[newItemPosition]
        }
        val diffResult = DiffUtil.calculateDiff(diffCallback)
        items.clear()
        items.addAll(newItems)
        diffResult.dispatchUpdatesTo(this)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SupplementViewHolder {
        val binding = ItemSupplementBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return SupplementViewHolder(binding)
    }

    override fun onBindViewHolder(holder: SupplementViewHolder, position: Int) {
        holder.bind(items[position])
    }

    override fun getItemCount() = items.size

    inner class SupplementViewHolder(private val binding: ItemSupplementBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: Supplement) {
            val text = item.sentence
            val spannableString = SpannableString(text)
            
            // 设置高亮文本的颜色（深粉色）
            item.highlights.forEach { highlight ->
                val startIndex = text.indexOf(highlight)
                if (startIndex != -1) {
                    spannableString.setSpan(
                        ForegroundColorSpan(Color.parseColor("#FF1493")), // 深粉色
                        startIndex,
                        startIndex + highlight.length,
                        SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE
                    )
                }
            }
            
            binding.sentenceText.text = spannableString
            
            // 动态显示或隐藏备注
            if (item.notes.isNullOrEmpty()) {
                binding.noteText.visibility = View.GONE
            } else {
                binding.noteText.visibility = View.VISIBLE
                binding.noteText.setText(item.notes.joinToString("\n"))
            }
            
            // 动态显示或隐藏标签
            with(binding.topicsLayout) {
                removeAllViews()
                if (item.highlights.isNotEmpty()) {
                    item.highlights.forEach { highlight ->
                        val chip = Chip(context).apply {
                            setText(highlight)
                            isClickable = false
                            setTextColor(Color.BLACK)
                            chipBackgroundColor = ColorStateList.valueOf(Color.parseColor("#E3F2FD"))
                        }
                        addView(chip)
                    }
                    visibility = View.VISIBLE
                } else {
                    visibility = View.GONE
                }
            }
        }
    }
} 